<template>
  <el-dialog :title="action === 'add' ? '新增' : '修改'" width="40%" draggable 
              :close-on-click-modal="false" v-model="visible" @open="submitted = false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmit(dataFormRef)" label-width="90px">
      <el-form-item label="仓库名" prop="name">
        <wareinfo-select v-model="dataForm.wareId" ref="wareinfoSelectRef"></wareinfo-select>
      </el-form-item>
      <el-form-item label="SKU" prop="skuId">
        <sku-select v-model="dataForm.skuId" ref="skuSelectRef"></sku-select>
      </el-form-item>
      <el-form-item label="采购数量" prop="num">
        <el-input-number v-model.number="dataForm.num" :min="0" :step="1" style="width: 100%" />
      </el-form-item>
      <el-form-item label="采购金额" prop="price">
        <el-input-number v-model.number="dataForm.price" :min="0" :precision="2" :step="1" style="width: 100%" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="ad-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit(dataFormRef)" :disabled="submitted">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import http from "@/utils/httpRequest";
import validate from "@/utils/validate";
import { ElMessageBox, FormInstance } from "element-plus";
import { ref, reactive, nextTick, defineExpose, defineEmits } from "vue";

import SkuSelect from "../common/sku-select.vue";
import WareinfoSelect from "../common/wareinfo-select.vue";

// 操作：add-新增，update-修改
const action = ref("");

// 弹窗是否显示
const visible = ref(false);

// 提交状态
const submitted = ref(false);

// 表单数据
const dataForm = reactive({
  id: undefined,
  skuId: "",
  wareId: "",
  num: 0,
  price: 0,
  status: 0,
});

// 数据校验规则
const dataRule = reactive({
  skuId: validate.required("SKU"),
  wareId: validate.required("仓库"),
  num: validate.required("采购数量"),
  price: validate.required("采购金额"),
});

const skuSelectRef = ref();

const wareinfoSelectRef = ref();

// 表单引用
const dataFormRef = ref<FormInstance>();

// 父级组件的自定义事件
const emit = defineEmits(['loadTableData']);

// 表单初始化
const initForm = async (action, data) => {
  action = action;
  // 新增操作
  if (action === "add") {
    visible.value = true;
    await nextTick();
    dataFormRef.value.resetFields();
    return;
  }
  // 修改操作
  dataForm.id = data.id;
  http
  .post({
    url: `/ware/purchase-detail/info?purchaseDetailId=${dataForm.id}`,
  })
  .then(async ({ data }) => {
    if (http.success(data)) {
      visible.value = true;
      await nextTick();
      dataFormRef.value.resetFields();
      let form = data.body;
      Object.keys(form).forEach(function (key) {
        dataForm[key] = form[key];
      });
      skuSelectRef.value.init(dataForm.skuId);
      wareinfoSelectRef.value.init(dataForm.wareId);
    }
  });
};

// 表单提交
const dataFormSubmit = (form: FormInstance | undefined) => {
  if (submitted.value) {
    return;
  }
  form.validate((valid) => {
    if (valid) {
      submitted.value = true;
      http
        .post({
          url: `/ware/purchase-detail/save`,
          data: dataForm,
        })
        .then(({ data }) => {
          if (http.success(data, true, true)) {
            visible.value = false;
            emit("loadTableData");
            return;
          }
          submitted.value = false;
        });
    }
  });
};

// 暴露方法
defineExpose({ initForm });
</script>
